package cn.edu.neusoft.food229.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.food229.Commen.Commen;
import cn.edu.neusoft.food229.R;
import cn.edu.neusoft.food229.activity.ShopActivity;
import cn.edu.neusoft.food229.bean.ShopBean;

public class ShopAdapter extends RecyclerView.Adapter {
    private List<ShopBean> list;
    private Context context;
    private LayoutInflater layoutInflater;
    public ShopAdapter(Context context,List<ShopBean>list){
        this.context=context;
        this.list=list;
        layoutInflater=LayoutInflater.from(context);
    }
    public void  setList(List<ShopBean> list){
        this.list=list;
        notifyDataSetChanged();
    }
    public class ViewHolder extends  RecyclerView.ViewHolder{
        ImageView imageView;
        TextView tv_name,tv_address;
        RatingBar bar;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imageView8);
            tv_name=itemView.findViewById(R.id.textView6);
            tv_address=itemView.findViewById(R.id.textView7);
            bar=itemView.findViewById(R.id.ratingBar);
        }
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v=layoutInflater.from(layoutInflater.getContext()).inflate(R.layout.item,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
       final ShopBean bean=list.get(position);
       if(bean==null)return;
        ViewHolder viewHolder=(ViewHolder) holder;
        Picasso.with(context)
                .load(Commen.BASEURL+bean.getPic())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);

        viewHolder.tv_name.setText(bean.getShopname());
        viewHolder.tv_address.setText(bean.getAddress());
       viewHolder.bar.setRating(bean.getLevel());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "详情:"+bean.getIntro()+"  订餐电话："+bean.getPhonenum(), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, ShopActivity.class);
                intent.putExtra("shop_id",bean.getShop_id());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
//                intent.putExtra("resid",bean.getId());
//                intent.putExtra("authorid",bean.getUserid());
//                context.startActivity(intent);

//                跳转到详情界面
            }
        });
//        viewHolder.button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Toast.makeText(context, "购买", Toast.LENGTH_SHORT).show();
//                Intent intent=new Intent(context, ViewArticleActivity.class);
//                intent.putExtra("resid",bean.getId());
//                intent.putExtra("authorid",bean.getUserid());
//                context.startActivity(intent);
//
//
//                跳转到购买页面
//            }
//        });

    }

    @Override
    public int getItemCount() {
        if(list!=null){
            return list.size();}//若使用对象是，避免出现空指针
        else
            return 0;//空条目是返回0
    }
}
